import { _decorator, Component, Node, Prefab, ProgressBar, CCInteger, CCFloat, AudioClip, Vec2, director, Size, sys, UITransform, instantiate, v2, Label, v3, view, Vec3, Rect, PageView, tween, randomRangeInt, Tween, find, } from 'cc';
// import LianLianKan_Game from './LianLianKan_Game';
import { LianLianKan_GameStorage } from './Data/LianLianKan_GameStorage';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import { SS_GameEventData } from 'db://assets/Script/Manager/SSS_GameEventManager';
import LianLianKan_GameData from './LianLianKan_GameData';
import { SSS_Consts } from 'db://assets/Script/Data/SSS_Consts';
import { LianLianKan } from './LianLianKan';
import { SSS_Lockable } from 'db://assets/Script/Common/SSS_Lockable';
import SSS_ShuaiFunction from 'db://assets/Script/Utils/SSS_ShuaiFunction';

const { ccclass, property } = _decorator;


@ccclass('LianLianKan_GamePanel')
export default class LianLianKan_GamePanel extends Component {
        static Instance: LianLianKan_GamePanel;

        // @property(Node)
        // public Button_start: Node | null = null;
        // @property(Node)
        // public Button_find: Node | null = null;
        // @property(Node)
        // public Button_refresh: Node | null = null;
        // @property(Node)
        // public Button_next: Node | null = null;

        @property(Label)
        Label_level: Label | null = null;

        @property(Node)
        Button_back: Node | null = null;
        @property(Node)
        Button_refresh: Node | null = null;

        @property(Node)
        Button_提示: Node | null = null;
        @property(Node)
        Button_打乱: Node | null = null;

        @property(Label)
        Label_提示_number: Label | null = null;
        @property(Node)
        提示广告icon: Node | null = null;

        @property(Label)
        Label_打乱_number: Label | null = null;
        @property(Node)
        打乱广告icon: Node | null = null;

        onLoad() {
                LianLianKan_GamePanel.Instance = this;
                // this.AddListener();
        }

        ShowOn() {
                this.node.active = true;
                this.AddListener();
                this.Refresh();
                this.Label_level.string = `第${LianLianKan_GameData.Get_level()}关`;
        }

        Refresh() {
                this.Label_提示_number.string = `${LianLianKan_GameStorage.data.mode_1.item_提示_number}`;
                this.Label_打乱_number.string = `${LianLianKan_GameStorage.data.mode_1.item_打乱_number}`;

                this.提示广告icon.active = LianLianKan_GameStorage.data.mode_1.item_提示_number <= 0;
                this.打乱广告icon.active = LianLianKan_GameStorage.data.mode_1.item_打乱_number <= 0;
        }

        AddListener() {
                this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
                this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

                this.Button_提示.on(Node.EventType.TOUCH_END, this.On_Button_提示, this);
                this.Button_打乱.on(Node.EventType.TOUCH_END, this.On_Button_打乱, this);

                // this.Button_start.on(Node.EventType.TOUCH_END, this.On_Button_start, this);
                // this.Button_find.on(Node.EventType.TOUCH_END, this.On_Button_find, this);
                // this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
                // this.Button_next.on(Node.EventType.TOUCH_END, this.On_Button_next, this);
        }
        RemoveListener() {
                this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
                this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

                this.Button_提示.off(Node.EventType.TOUCH_END, this.On_Button_提示, this);
                this.Button_打乱.off(Node.EventType.TOUCH_END, this.On_Button_打乱, this);
        }

        On_Button_back() {
                director.loadScene(SSS_Consts.SceneName.MainScene);
                SSS_AudioManager.PlayButtonAudio();
        }

        On_Button_refresh() {
                console.log(`On_Button_refresh`);

                LianLianKan.Instance.Replay();

                SSS_AudioManager.PlayButtonAudio();
        }

        @SSS_Lockable(1000)
        On_Button_提示() {
                if (LianLianKan_GameStorage.data.mode_1.item_提示_number > 0) {
                        LianLianKan_GameStorage.data.mode_1.item_提示_number--;
                        LianLianKan_GameStorage.saveData();
                        this.Refresh();

                        LianLianKan.Instance.showHint();
                }
                else {
                        SSS_ShuaiFunction.AD_callback(() => {
                                this.增加道具撤回次数();
                        });


                        // AdManager.是否看完广告 = false;
                        // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
                        //         () => {
                        //                 director.emit(SS_GameEventData.广告结束);

                        //                 this.增加道具撤回次数();
                        //         },
                        //         () => {
                        //                 director.emit(SS_GameEventData.广告结束);

                        //                 console.log("广告没看完！");
                        //         },
                        // );
                }

                SSS_AudioManager.PlayButtonAudio();
        }

        @SSS_Lockable(1000)
        On_Button_打乱() {
                if (LianLianKan_GameStorage.data.mode_1.item_打乱_number > 0) {
                        LianLianKan_GameStorage.data.mode_1.item_打乱_number--;
                        LianLianKan_GameStorage.saveData();
                        this.Refresh();

                        LianLianKan.Instance.reshuffle();
                }
                else {
                        SSS_ShuaiFunction.AD_callback(() => {
                                this.增加道具翻转次数();
                        });


                        // AdManager.是否看完广告 = false;
                        // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
                        //         () => {
                        //                 director.emit(SS_GameEventData.广告结束);

                        //                 this.增加道具翻转次数();
                        //         },
                        //         () => {
                        //                 director.emit(SS_GameEventData.广告结束);

                        //                 console.log("广告没看完！");
                        //         },
                        // );
                }

                SSS_AudioManager.PlayButtonAudio();
        }

        增加道具撤回次数() {
                LianLianKan_GameStorage.data.mode_1.item_提示_number += LianLianKan_GameData.item_提示_add_number;
                LianLianKan_GameStorage.saveData();
                this.Refresh();
        }
        增加道具翻转次数() {
                LianLianKan_GameStorage.data.mode_1.item_打乱_number += LianLianKan_GameData.item_打乱_add_number;
                LianLianKan_GameStorage.saveData();
                this.Refresh();
        }

        // On_Button_start() {
        //         this.游戏开始();
        // }
        // On_Button_find() {
        //         LianLianKan_Game.Instance.findTwoNode();
        // }
        // On_Button_refresh() {
        //         LianLianKan_Game.Instance.refreshBlock();
        // }
        // On_Button_next() {
        //         LianLianKan_Game.Instance.NextLevel();
        // }

        // 游戏开始() {
        //         LianLianKan_Game.Instance.gamePlay();
        // }
}